﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.IO;
using System.Windows.Media.Imaging;
using ExplorerInfrastructure;

namespace BaseTypesControlLib.Converters
{
    public class BitmapPathConverter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string relPath = value as string;
            string rootPath = Path.GetDirectoryName(ProjectInfo.GetInstance().Path);
            relPath = (relPath == null ? "" : relPath);
            relPath = relPath.Replace('/', '\\');
            rootPath = (rootPath == null ? "" : rootPath);

            string path = Path.Combine(rootPath, relPath);
            if (File.Exists(path))
            {
                BitmapImage img = new BitmapImage(new Uri(path, UriKind.RelativeOrAbsolute));
                return img;
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
